
#pragma once

namespace cusp
{
namespace io
{

/*! \addtogroup input_output Input/Output
 *  \addtogroup dense_matrix DenseMatrix
 *  \ingroup input_output
 *  \{
 */

/*! \brief Write all the elements of a matrix to a stream.
 *
 * This is suited for writing csv file or Matlab scripts.
 *
 * \param m a matrix
 * \param out output stream
 * \param colSep column separator
 * \param rowSep row separator
 *
 * \code
 * cusp::array2d<float, cusp::host_memory> A;
 * std::fostream out("script.m");
 *
 * ...
 *
 * out << "A = [ ";
 * cusp::io::write_dense_matrix(A, out);
 * out << "];" << std::endl;
 * \endcode
 */
template<typename MatrixType>
std::ostream& write_dense_matrix(const MatrixType& m,
		std::ostream& out = std::cout,
		const std::string& colSep = " ",
		const std::string& rowSep = ";")
{
	for (size_t i = 0; i < m.num_rows; ++i)
	{
		if (m.num_cols > 0) out << m(i,0);
		for (size_t j = 1; j < m.num_cols; ++j)
		{
			out << colSep << m(i,j);
		}
		if (i < m.num_rows - 1) out << rowSep;
	}

	return out;
}

/*! \}
 */

} //end namespace io
} //end namespace cusp

